using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace MicroMsg.UI.ImageFilter
{
	public class ImageFilterMgr
	{
		public const string prefix = "Resource/Img/ImageFilter";
		private int mSrcImgWidth;
		private int mSrcImgHeight;
		public int[] mSrcRgbA32;
		public WriteableBitmap mSrcImg;
		public byte[,] mSrcRGB_t;
		public byte[,] mRGB_t;
		public List<byte[,]> mMasks;
		private static Dictionary<string, FilterSetting> filterSettingMap;
		private bool ReadSrcImgFlag;
		public static Dictionary<string, FilterSetting> GetFilterSettingMap()
		{
			if (ImageFilterMgr.filterSettingMap == null)
			{
				ImageFilterMgr.initFilterSettingMap();
			}
			return ImageFilterMgr.filterSettingMap;
		}
		private static void initFilterSettingMap()
		{
			ImageFilterMgr.filterSettingMap = new Dictionary<string, FilterSetting>();
			ImageFilterMgr.filterSettingMap.set_Item("Normal", new FilterSetting
			{
				string_0 = "Normal",
				name = "原图",
				maskName = null,
				icon = "Resource/Img/ImageFilter/icon/normal.png",
				handler = null
			});
			ImageFilterMgr.filterSettingMap.set_Item("LOMO", new FilterSetting
			{
				string_0 = "LOMO",
				name = "LOMO",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/LOMO_00.jpg",
					"Resource/Img/ImageFilter/mask/LOMO_01.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/LOMO.png",
				handler = new ImageFilterHandler(FilterMethod.LOMO)
			});
			ImageFilterMgr.filterSettingMap.set_Item("Wheat", new FilterSetting
			{
				string_0 = "Wheat",
				name = "麦田",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/Wheat_00.jpg",
					"Resource/Img/ImageFilter/mask/Wheat_01.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/fisheye.png",
				handler = new ImageFilterHandler(FilterMethod.Wheat)
			});
			ImageFilterMgr.filterSettingMap.set_Item("Glossy", new FilterSetting
			{
				string_0 = "Glossy",
				name = "玻璃镜",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/Glossy_00.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/glossy.png",
				handler = new ImageFilterHandler(FilterMethod.Glossy)
			});
			ImageFilterMgr.filterSettingMap.set_Item("Aquatint", new FilterSetting
			{
				string_0 = "Aquatint",
				name = "铜版画",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/Aquatint_00.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/copperplate.png",
				handler = new ImageFilterHandler(FilterMethod.Aquatint)
			});
			ImageFilterMgr.filterSettingMap.set_Item("Lake", new FilterSetting
			{
				string_0 = "Lake",
				name = "湖水",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/Lake_00.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/norwegian.png",
				handler = new ImageFilterHandler(FilterMethod.Lake)
			});
			ImageFilterMgr.filterSettingMap.set_Item("Twilight", new FilterSetting
			{
				string_0 = "Twilight",
				name = "黄昏",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/Twilight_00.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/twilight.png",
				handler = new ImageFilterHandler(FilterMethod.Twilight)
			});
			ImageFilterMgr.filterSettingMap.set_Item("BlackWhite", new FilterSetting
			{
				string_0 = "BlackWhite",
				name = "黑白",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/BlackWhite_00.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/blackWhite.png",
				handler = new ImageFilterHandler(FilterMethod.BlackWhite)
			});
			ImageFilterMgr.filterSettingMap.set_Item("Pen", new FilterSetting
			{
				string_0 = "Pen",
				name = "圆珠笔",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/Pen_00.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/painting.png",
				handler = new ImageFilterHandler(FilterMethod.Pen)
			});
			ImageFilterMgr.filterSettingMap.set_Item("Polaroid", new FilterSetting
			{
				string_0 = "Polaroid",
				name = "拍立得",
				maskName = new string[]
				{
					"Resource/Img/ImageFilter/mask/Polaroid_00.jpg",
					"Resource/Img/ImageFilter/mask/Polaroid_01.jpg"
				},
				icon = "Resource/Img/ImageFilter/icon/polaroid.png",
				handler = new ImageFilterHandler(FilterMethod.Polaroid)
			});
			ImageFilterMgr.filterSettingMap.set_Item("Poster", new FilterSetting
			{
				string_0 = "Poster",
				name = "海报",
				maskName = null,
				icon = "Resource/Img/ImageFilter/icon/poster.png",
				handler = new ImageFilterHandler(FilterMethod.Poster)
			});
		}
		public ImageFilterMgr(WriteableBitmap srcIamge)
		{
			if (ImageFilterMgr.filterSettingMap == null)
			{
				ImageFilterMgr.initFilterSettingMap();
			}
			this.mSrcImg = srcIamge;
		}
		public WriteableBitmap ProcessImg(string filterType)
		{
			if (filterType == null)
			{
				return null;
			}
			if (!ImageFilterMgr.filterSettingMap.ContainsKey(filterType))
			{
				return null;
			}
			this.ResetOrignalImg();
			this.mMasks = null;
			this.LoadMasks(filterType);
			if (ImageFilterMgr.filterSettingMap.get_Item(filterType).handler != null)
			{
				ImageFilterMgr.filterSettingMap.get_Item(filterType).handler(this.mRGB_t, this.mMasks, this.mSrcImgHeight * this.mSrcImgWidth);
			}
			return this.CreateImg(this.mRGB_t);
		}
		private void ResetOrignalImg()
		{
			this.mRGB_t = (byte[,])this.mSrcRGB_t.Clone();
		}
		public void ReadSrcImg()
		{
			if (this.ReadSrcImgFlag)
			{
				return;
			}
			this.mSrcImgWidth = this.mSrcImg.get_PixelWidth();
			this.mSrcImgHeight = this.mSrcImg.get_PixelHeight();
			this.mSrcRgbA32 = this.mSrcImg.get_Pixels();
			this.mSrcRGB_t = new byte[this.mSrcImgWidth * this.mSrcImgHeight, 4];
			int num = this.mSrcImgHeight * this.mSrcImgWidth;
			for (int i = 0; i < num; i++)
			{
				byte[] bytes = BitConverter.GetBytes(this.mSrcImg.get_Pixels()[i]);
				this.mSrcRGB_t[i, 0] = bytes[0];
				this.mSrcRGB_t[i, 1] = bytes[1];
				this.mSrcRGB_t[i, 2] = bytes[2];
				this.mSrcRGB_t[i, 3] = bytes[3];
			}
			this.ReadSrcImgFlag = true;
		}
		private void LoadMasks(string filterType)
		{
			if (ImageFilterMgr.filterSettingMap.get_Item(filterType).maskName != null && ImageFilterMgr.filterSettingMap.get_Item(filterType).maskName.Length != 0)
			{
				if (this.mMasks == null)
				{
					this.mMasks = new List<byte[,]>();
				}
				for (int i = 0; i < ImageFilterMgr.filterSettingMap.get_Item(filterType).maskName.Length; i++)
				{
					string text = ImageFilterMgr.filterSettingMap.get_Item(filterType).maskName[i];
					Uri uri = new Uri(text, 2);
					StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
					Stream stream = resourceStream.get_Stream();
					MemoryStream memoryStream = new MemoryStream();
					Util.reSizePic(stream, memoryStream, this.mSrcImgWidth, this.mSrcImgHeight, 100);
					BitmapImage bitmapImage = new BitmapImage();
					bitmapImage.SetSource(memoryStream);
					WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);
					int pixelWidth = writeableBitmap.get_PixelWidth();
					int pixelHeight = writeableBitmap.get_PixelHeight();
					byte[,] array = new byte[pixelWidth * pixelHeight, 4];
					this.mMasks.Add(array);
					for (int j = 0; j < pixelWidth * pixelHeight; j++)
					{
						byte[] bytes = BitConverter.GetBytes(writeableBitmap.get_Pixels()[j]);
						array[j, 0] = bytes[2];
						array[j, 1] = bytes[1];
						array[j, 2] = bytes[0];
						array[j, 3] = bytes[3];
					}
				}
				return;
			}
		}
		private WriteableBitmap CreateImg(byte[,] byte_0)
		{
			if (this.mSrcImgHeight * this.mSrcImgWidth != byte_0.GetLength(0))
			{
				return null;
			}
			WriteableBitmap writeableBitmap = new WriteableBitmap(this.mSrcImgWidth, this.mSrcImgHeight);
			byte[] array = new byte[4];
			for (int i = 0; i < this.mSrcImgHeight * this.mSrcImgWidth; i++)
			{
				array[0] = byte_0[i, 0];
				array[1] = byte_0[i, 1];
				array[2] = byte_0[i, 2];
				array[3] = byte_0[i, 3];
				writeableBitmap.get_Pixels()[i] = BitConverter.ToInt32(array, 0);
			}
			return writeableBitmap;
		}
		public static bool IsSettingAutoSavePhotos()
		{
			return ConfigMgr.getBool("Set_Common_AutoSavePhotos", true);
		}
	}
}
